package com.app.micaihu.bean.news;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.adapter.NewsAdapter;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.listener.AutoWidthControllerListener;
import com.app.utils.util.image.LoadImageUtils;

/**
 * 新闻列表UI的父类
 * Created by linkai on 2016/7/17.
 */
public class ViewNewsParent {

    //根布局
    public View mConvertView;
    // title
    public TextView title;
    // 评论数量
    public TextView commentCount;
    // 发布时间
    public TextView publishTime;
    //推广类型图标
    public CustomImageView mark;

    //推广类型小标的加载监听
    AutoWidthControllerListener autoWidthControllerListener;


    /**
     * 初始化公共布局
     *
     * @param convertView
     */
    protected void initView(View convertView) {
        this.mConvertView = convertView;
        this.title = (TextView) convertView.findViewById(R.id.item_public_title);
        this.commentCount = (TextView) convertView.findViewById(R.id.item_public_comment_count);
        this.publishTime = (TextView) convertView.findViewById(R.id.item_public_time);
        this.mark = (CustomImageView) convertView.findViewById(R.id.item_public_alt_mark);
        autoWidthControllerListener = new AutoWidthControllerListener(this.mark);
    }

    /**
     * 加载数据
     *
     * @param titleStr
     * @param matchStr 匹配的字符串
     */
    protected void matchTitle(String titleStr, String matchStr) {
        if (TextUtils.isEmpty(matchStr) || TextUtils.isEmpty(titleStr)) {
            return;
        }
        int startIndex = titleStr.toLowerCase().indexOf(matchStr.toLowerCase());
        if (startIndex >= 0) {
            //开始匹配红色字符
            int endIndex = startIndex + matchStr.length();
            String oneStr = titleStr.substring(0, startIndex);
            if (endIndex > titleStr.length() - 1) {
                endIndex = titleStr.length() - 1;
            }
            String twoStr = titleStr.substring(endIndex);
            matchStr = titleStr.substring(startIndex, endIndex);
            String str;
            str = oneStr + "<font color='#E55D5D'>" + matchStr + "</font>" + twoStr;
            this.title.setText(Html.fromHtml(str));
        }

    }

    /**
     * 加载公共数据
     *
     * @param newsEntity
     */
    protected void loadData(NewsEntity newsEntity) {

        if (this.title != null) {
            this.title.setText(newsEntity.getArticleTitle());
            //判断是否已读
            if (NewsAdapter.isReadNews(newsEntity.getArticleId())) {
                this.title.setEnabled(false);
            } else {
                this.title.setEnabled(true);
            }
        }

        if (publishTime != null) {
            this.publishTime.setText(newsEntity.getArticleTime());
        }
        if (commentCount != null && TextUtils.isEmpty(newsEntity.getCommentNum())) {
            this.commentCount.setVisibility(View.GONE);
        } else if (commentCount != null) {
            this.commentCount.setVisibility(View.VISIBLE);
            this.commentCount.setText(newsEntity.getCommentNum() + "评论");
        }
        //加载小标
        if (mark != null && TextUtils.isEmpty(newsEntity.getArticleMark())) {
            if (this.mark.getVisibility() == View.VISIBLE) {
                this.mark.setVisibility(View.GONE);
            }
        } else {
            if (this.mark.getVisibility() == View.GONE) {
                this.mark.setVisibility(View.VISIBLE);
            }
            LoadImageUtils.getInstance().loadImage(this.mark, newsEntity.getArticleMark(), autoWidthControllerListener);
        }

    }

    /**
     * 加载公共数据
     *
     * @param newsEntity
     */
    protected void loadDataNoMark(NewsEntity newsEntity) {

        if (this.title != null) {
            this.title.setText(newsEntity.getArticleTitle());
            //判断是否已读
            if (NewsAdapter.isReadNews(newsEntity.getArticleId())) {
                this.title.setEnabled(false);
            } else {
                this.title.setEnabled(true);
            }
        }

        if (publishTime != null) {
            this.publishTime.setText(newsEntity.getArticleTime());
        }
        if (commentCount != null && TextUtils.isEmpty(newsEntity.getCommentNum())) {
            this.commentCount.setVisibility(View.GONE);
        } else if (commentCount != null) {
            this.commentCount.setVisibility(View.VISIBLE);
            this.commentCount.setText(newsEntity.getCommentNum() + "评论");
        }
    }

}
